
import React , {Component} from 'react'

import {URI_LOGIN} from "../const/shareUri";
import Ajax from "../assets/js/fetch"

import SHA256 from 'crypto-js/sha256'

import '../styles/Login.less'

export default class Login extends Component{
  constructor(){
    super();
    this.state={
      userName:'',
      password:''
    }
  }
  componentDidMount(){

  }
  loginClick(){
    console.log(this.state.password);
    Ajax.post(URI_LOGIN,{
      phoneNumber: this.state.userName,
      password: SHA256(this.state.password).toString(),
      loginType:1
    },res=>{
      console.log(res);
      window.localStorage.setItem('qqlgClientAccessToken', res.data.accessToken);
      window.localStorage.setItem('qqlgClientUserId', res.data.id);
      window.localStorage.setItem('qqlgClientNickName', res.data.nickName);
      alert('登录成功');
      //window.history.go(-1);
    })
  }
  userNameOchange(){
    console.log(this.refs.user.value);
    let userName = this.refs.user.value;
    this.setState({userName:userName})
  }
  passWordOchange(){
    let password = this.refs.password.value;
    this.setState({password:password})
  }
  render(){
    return (
        <div className="login">
          <h3>用户登录</h3>
          <input type="text" ref='user' onChange={this.userNameOchange.bind(this)} placeholder="用户名"/>
          <input className="middle" ref="password" onChange={this.passWordOchange.bind(this)} type="password" placeholder="密码"/>
          <input className="button" type="button" onClick={this.loginClick.bind(this)} value="登录"/>
        </div>
    )
  }
}